package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.qo.UserContext;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.util.NoticeEnum;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IEmployeeService employeeService;

    @RequiredPermission(name = "公告页面", expression = "notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo) {
        Employee employee = UserContext.getEmployee();
        //获取当前登录人的id 获取查看的数据
        Long id = employee.getId();
        //如果有权限可以查看 未发布 和 发布的
        boolean admin = employee.isAdmin();
        //共享权限  通过if 隐藏 按钮
        model.addAttribute("admin", admin);

        //级别获取
        model.addAttribute("enums", NoticeEnum.values());
        PageInfo<Notice> pageInfo = noticeService.query(qo, id, admin);
        model.addAttribute("pageInfo", pageInfo);

        //获取已读人数 和 未读人数
        List<Notice> notices = noticeService.readCount();
        model.addAttribute("notices", notices);
        return "notice/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name = "公告删除", expression = "notice:delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        if (id != null) {
            noticeService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "公告新增/编辑", expression = "notice:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Notice notice) {

        if (notice.getId() != null) {
            noticeService.update(notice);
        } else {
            noticeService.save(notice);
        }
        return new JsonResult();
    }

    @RequestMapping("/updateStatus")
    @RequiredPermission(name = "公告发布修改", expression = "notice:updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id, Boolean status) {
        if (status == null) {
            status = true;
        } else {
            status = false;
        }
        noticeService.updateStatus(id, status);
        return new JsonResult();
    }

    @RequiredPermission(name = "详情页面", expression = "notice:show")
    @RequestMapping("/show")
    public String show(Model model, Long id) {
        //通过id去获取数据
        Notice notice = noticeService.get(id);
        model.addAttribute("notice", notice);
        Long empId = notice.getEmployee().getId();
        Employee employee = employeeService.selectEmployee(empId);

        model.addAttribute("employeeName", employee.getName());
        if (notice.getIsRead() == null) {
            noticeService.updateIsRead(id, UserContext.getEmployee().getId(), true);
        }
        return "notice/show";
    }

    @RequestMapping("/getUnread")
    @ResponseBody
    public JsonResult getUnread() {
        Integer count = noticeService.getUnread(UserContext.getEmployee().getId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(count);
        return jsonResult;
    }
}
